package com.sds.sweng.swat.miplatform.ui.design.parts.component;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;

import com.sds.sweng.swat.miplatform.ui.design.figures.TabFigureFactory;
import com.sds.sweng.swat.miplatform.ui.design.models.area.sub.TabContentsSubArea;
import com.sds.sweng.swat.miplatform.ui.design.models.component.MTab;
import com.sds.sweng.swat.miplatform.ui.design.parts.MiplatformComponentPart;
import com.sds.sweng.swat.miplatform.ui.design.parts.area.sub.TabContentsSubAreaPart;
import com.sds.sweng.swat.miplatform.ui.design.policies.DesignComponentEditPolicy;
import com.sds.sweng.swat.miplatform.ui.design.policies.TabSelectionEditPolicy;

public class MTabPart extends MiplatformComponentPart {

	public boolean isSelectable() {
		return true;
	}

	protected IFigure createFigure() {
		MTab model = (MTab) getModel();
		model.getTabContents().getForm().setId(getRootModel().getForm().getId() + "tv");

		if (model.isSelected()) {
			if (getParent().getParent().getParent().getChildren().size() > 1) {
				TabContentsSubAreaPart tabContentsSubAreaPart =
					(TabContentsSubAreaPart) getParent().getParent().getParent().getChildren().get(1);
				TabContentsSubArea tabContentsSubArea = (TabContentsSubArea) tabContentsSubAreaPart.getModel();

				tabContentsSubArea.setContents(model.getTabContents());
			}
		}

		return TabFigureFactory.create(model.getType(), model.getText(), model.isSelected());
	}

	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new DesignComponentEditPolicy());
		installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, new TabSelectionEditPolicy());
	}

	protected void refreshVisuals() {
		MTab model = (MTab) getModel();

		if (model.isSelected()) {
			if (getParent().getParent().getParent().getChildren().size() > 1) {
				TabContentsSubAreaPart tabContentsSubAreaPart =
					(TabContentsSubAreaPart) getParent().getParent().getParent().getChildren().get(1);
				TabContentsSubArea tabContentsSubArea = (TabContentsSubArea) tabContentsSubAreaPart.getModel();

				tabContentsSubArea.setContents(model.getTabContents());
			}
		}
	}

}
